import shortuuid

from ..database import database
from ..models.auth import Auth
from ..models.local_auth import LocalAuth


class UserService(object):
    @classmethod
    def get_user(cls, id):
        return Auth.query.filter(Auth.id == id).first()

    @classmethod
    def create_user_and_local_auth(cls, screen_name, type, id=None, head_image=None, login_name=None, login_password=None, *args,
                                   **kwargs):
        user_id = id if id  else shortuuid.uuid()
        kwargs.update({
            'screen_name': screen_name,
            'type': type,
            'head_image': head_image if head_image else '',
            'id': user_id
        })
        user = Auth(**kwargs)
        database.session.add(user)

        if login_name:
            local_auth = LocalAuth(login_name=login_name,

                                   type=type,
                                   password=LocalAuth.encrypt_password(login_password))
            local_auth.auth = user
        database.session.add(local_auth)
        database.session.commit()
        return user
